package cn.gudian.cms.News

class ArticleController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    def mode = params.mode
    if (!mode) mode = "Article"
    def offset = params.offset?.toInteger()
    def max = params.max?.toInteger()
    if (!offset) offset = (params.offset ? params.offset.toInteger() : 0)
    if (!max) max = (params.max ? params.max.toInteger() : 10)

    def arcTypeID = params.arcTypeID
    def articleInstanceList = null;
    def articleInstanceTotal = null;
    if (arcTypeID) {
      articleInstanceList = Article.findAll("from Article where arc_type_id=? order by id desc", [arcTypeID], [max: max, offset: offset])
      articleInstanceTotal = Article.executeQuery("select count(*) from Article where arc_type_id =?", [arcTypeID])[0]
    } else {
      articleInstanceList = Article.findAll("from Article where arc_type_id in(select id from ArcType where mode=?) order by id desc", [mode], [max: max, offset: offset])
      articleInstanceTotal = Article.executeQuery("select count(*) from Article where arc_type_id in(select id from ArcType where mode=?)", [mode])[0]
    }
    [mode: mode,arcTypeID:arcTypeID, articleInstanceList: articleInstanceList, articleInstanceTotal: Article.executeQuery("select count(*) from Article where arc_type_id in(select id from ArcType where mode=?)", [mode])[0]]
  }

  def create = {
    def mode = params.mode
    if (!mode) mode = "Article"
    def articleInstance = new Article()
    articleInstance.properties = params
    return [mode: mode, articleInstance: articleInstance]
  }

  def save = {
    //取得最大排序值
    def orderNum = Article.executeQuery("select max(orderNum) from Article");
    if (orderNum[0] != null)
      orderNum = orderNum[0] + 1
    else
      orderNum = 0

    def mode = params.mode
    if (!mode) mode = "Article"
    def articleInstance = new Article(params)
    articleInstance.adddate = new Date();
    articleInstance.userip = request.getRemoteAddr();
    articleInstance.orderNum = orderNum
    articleInstance.ismake = false;
    if (!articleInstance.hasErrors() && articleInstance.save()) {
      flash.message = "article.created"
      flash.args = [articleInstance.id]
      flash.defaultMessage = "Article ${articleInstance.id} created"
      redirect(action: "list",  params: [mode: mode])
    }
    else {
      render(view: "create", model: [mode: mode, articleInstance: articleInstance])
    }
  }

  def show = {
    def mode = params.mode
    if (!mode) mode = "Article"
    def articleInstance = Article.get(params.id)
    if (!articleInstance) {
      flash.message = "article.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Article not found with id ${params.id}"
      redirect(action: "list", params: [mode: mode])
    }
    else {
      return [mode: mode, articleInstance: articleInstance]
    }
  }

  def edit = {
    def mode = params.mode
    if (!mode) mode = "Article"
    def articleInstance = Article.get(params.id)
    if (!articleInstance) {
      flash.message = "article.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Article not found with id ${params.id}"
      redirect(action: "list", params: [mode: mode])
    }
    else {
      return [mode: mode, articleInstance: articleInstance]
    }
  }

  def update = {
    def mode = params.mode
    if (!mode) mode = "Article"
    def articleInstance = Article.get(params.id)
    if (articleInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (articleInstance.version > version) {

          articleInstance.errors.rejectValue("version", "article.optimistic.locking.failure", "Another user has updated this Article while you were editing")
          render(view: "edit", model: [mode: mode, articleInstance: articleInstance])
          return
        }
      }
   
      articleInstance.properties = params

      if (!articleInstance.hasErrors() && articleInstance.save()) {
        flash.message = "article.updated"
        flash.args = [params.id]
        flash.defaultMessage = "Article ${params.id} updated"
        redirect(action: "list", params: [mode: mode])
      }
      else {
        render(view: "edit", model: [mode: mode, articleInstance: articleInstance])
      }
    }
    else {
      flash.message = "article.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Article not found with id ${params.id}"
      redirect(action: "edit", id: params.id, params: [mode: mode])
    }
  }

  def delete = {
    def mode = params.mode
    if (!mode) mode = "Article"
    def articleInstance = Article.get(params.id)
    if (articleInstance) {
      try {
        articleInstance.delete()
        flash.message = "article.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "Article ${params.id} deleted"
        redirect(action: "list", params: [mode: mode])
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "article.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "Article ${params.id} could not be deleted"
        redirect(action: "show", id: params.id, params: [mode: mode])
      }
    }
    else {
      flash.message = "article.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "Article not found with id ${params.id}"
      redirect(action: "list", params: [mode: mode])
    }
  }

  def deleteAll = {
    def mode = params.mode
    if (!mode) mode = "Article"
    def qstr = params.qstr
    if (qstr) {
      try {
        Article.executeUpdate("delete Article where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list", params: [mode: mode])
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list", params: [mode: mode])
      }
    }
  }
  def audit = {

    def mode = params.mode
    if (!mode) mode = "Article"
    def qstr = params.qstr
    if (qstr) {
      try {
        Article.executeUpdate("update Article set ismake=true where id in(" + qstr + ")")
        flash.message = "审核成功:" + qstr
        redirect(action: "list", params: [mode: mode])
      } catch (Exception e) {
        flash.message = "审核失败:" + qstr
        redirect(action: "list", params: [mode: mode])
      }
    }
  }
}
